package com.sino.dataworks.job;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.aliyun.odps.data.Record;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.utils.StringUtils;
import com.sino.dataworks.enums.ProjectEnums;
import com.sino.dataworks.util.DataworksUtil;
import com.sino.dataworks.util.JobVarUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

import java.util.*;

import static com.sino.dataworks.util.JobVarUtil.*;
@Slf4j
public class DataworksInstanceLog implements Job {

    static IAcsClient client = null;
    Map<String, String> warningMap = new HashMap<>();

    static {
        String endPoint = System.getenv("ALIBABA_END_POINT");
        String regionId = System.getenv("ALIBABA_REGION_ID");
        if (StringUtils.isEmpty(endPoint)) {
            endPoint = "dataworks-public-vpc.res.cloud.cheryfs.cn";
        }
        String keyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String keySec = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        if (StringUtils.isEmpty(keyId)) {
            keyId = "ZR18OFackif3jKT7";
        }
        if (StringUtils.isEmpty(keySec)) {
            keySec = "Pfvaw2BOmDPC4zEu1UglDu87jCn5yi";
        }
        if (StringUtils.isEmpty(regionId)) {
            regionId = "cn-wuhu-qrhy-d01";
        }
        DefaultProfile profile = DefaultProfile.getProfile(
                regionId,
                keyId, // 替换为你的AccessKey ID
                keySec); // 替换为你的AccessKey Secret
        DefaultProfile.addEndpoint(regionId, "dataworks-public", endPoint);
        client = new DefaultAcsClient(profile);
    }
    @Override
    public void execute(JobExecutionContext jobExecutionContext)  {
        Date newDate = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, -1);
        String bizDate = DateUtil.format(newDate, "yyyyMMdd");
        try {
            mailRecords=new ArrayList<>();
            log.info("数据日期:"+bizDate);
//            DataworksUtil.getSyncTaskDetail(1);
//            Integer totalNum= JobVarUtil.instaceListTotalCount;
//            int totalPages=(totalNum+100-1)/100;
//            for (int i=2;i<=totalPages;i++){
//                DataworksUtil.getSyncTaskDetail(i);
//                Thread.sleep(1000*60);
//            }
            for (ProjectEnums enums:ProjectEnums.values()){
                DataworksUtil.getDatasource(enums.getCode());
                DataworksUtil.getTaskLog(1,enums.getCode());
            }
            if(errorTask!=null&&errorTask.size()>0){
                for(String key:errorTask.keySet()){
                    List<Record> records=errorTask.get(key);
                    DataworksUtil.insertInstanceLog(bizDate,records,"dataops_run_batch_error_info");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
        //    DataworksUtil.getRecords(bizDate);
            DataworksUtil.gpsMonitor(bizDate,"device_history_kafka","gps_project");
      //      DataworksUtil.getDataSourceData(bizDate,"dw_datasource_info","datawarehouse_pro");
            hasRerunMap.clear();
            hasWarningMap.clear();
            errorTask.clear();
            retryMap.clear();
            begin=null;
            isFirstWarning=true;
            isNeedWarning=false;
            JobVarUtil.instanceMap.clear();
            JobVarUtil.instaceListTotalCount=0;
            JobVarUtil.instanceSyncTaskMap.clear();
            mailRecords=new ArrayList<>();
            retryCount=0;
            phoneWarnDtoMap.clear();
            oracleBegin=null;
            isAllCheck=false;
            JobVarUtil.databaseTotalCount=0;
            datasourceMap=new HashMap<>();
            warningDate=null;
        }
    }
}
